package com.hj.ct.db.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.hj.ct.db.DBConstants;
import com.hj.ct.model.SetAlarm;

public class SetAlarmDao extends SQLBaseDao {

	public SetAlarmDao(Context context) {
		super(context);
	}

	/**
	 * 插入数据
	 * 
	 * @param message
	 */
	public void insertSet(SetAlarm alarm) {
		db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		ContentValues values = getCV(alarm);
		db.insert(DBConstants.SQL_ALARM_TABLE, null, values);
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}

	/**
	 * 插入数据
	 * 
	 * @param message
	 */
	public void insertSet(List<SetAlarm> alarms) {
		if(alarms == null){
			return;
		}
		db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		int size = alarms.size();
		for (int i = 0; i < size; i++) {
			SetAlarm alarm = alarms.get(i);
			ContentValues values = getCV(alarm);
			db.insert(DBConstants.SQL_ALARM_TABLE, null, values);
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}

	public ContentValues getCV(SetAlarm alarm) {
		ContentValues values = new ContentValues();
		values.put("DeviceID", alarm.getDeviceID());
		values.put("DustAlarmType", alarm.getDustAlarmType());
		values.put("DustHi_Day", alarm.getDustHi_Day());
		values.put("DustHi_Night", alarm.getDustHi_Night());
		values.put("DustDelay", alarm.getDustDelay());

		values.put("NoiseAlarmType", alarm.getNoiseAlarmType());
		values.put("NoiseHi_Day", alarm.getNoiseHi_Day());
		values.put("NoiseHi_Night", alarm.getNoiseHi_Night());
		values.put("NoiseDelay", alarm.getNoiseDelay());

		values.put("DayTimeStart", alarm.getDayTimeStart());
		values.put("NightTimeStart", alarm.getNightTimeStart());

		return values;
	}

	/**
	 * 删除数据
	 * 
	 * @param message
	 */
	public void deleteAlarm(String devId) {
		db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		db.delete(DBConstants.SQL_ALARM_TABLE, "DeviceID=?",
				new String[] { devId });
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}

	/**
	 * 删除数据
	 * 
	 * @param message
	 */
	public void deleteAlarm() {
		db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		db.delete(DBConstants.SQL_ALARM_TABLE, null, null);
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}

	public HashMap<String, SetAlarm> selectAlarms() {
		HashMap<String, SetAlarm> businesses = new HashMap<String, SetAlarm>();
		db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(DBConstants.SQL_ALARM_TABLE, null, null, null,
				null, null, "id asc");
		while (cursor.moveToNext()) {
			SetAlarm alarm = buildAlarm(cursor);
			businesses.put(alarm.getDeviceID(), alarm);
		}
		db.close();
		return businesses;
	}

	public List<SetAlarm> selectAlarmsList() {
		List<SetAlarm> businesses = new ArrayList<SetAlarm>();
		db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(DBConstants.SQL_ALARM_TABLE, null, null, null,
				null, null, "id asc");
		while (cursor.moveToNext()) {
			businesses.add(buildAlarm(cursor));
		}
		db.close();
		return businesses;
	}

	public SetAlarm selectAlarms(String devId) {
		db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(DBConstants.SQL_ALARM_TABLE, null,
				"DeviceID = ?", new String[] { devId }, null, null, "id asc");
		if (cursor.moveToNext()) {
			return buildAlarm(cursor);
		}
		db.close();
		return null;
	}

	public SetAlarm buildAlarm(Cursor cursor){
		SetAlarm alarm = new SetAlarm();
		alarm.setId(cursor.getString(cursor.getColumnIndex("id")));
		
		alarm.setDeviceID(cursor.getString(cursor.getColumnIndex("DeviceID")));
		alarm.setDustAlarmType(cursor.getString(cursor.getColumnIndex("DustAlarmType")));
		alarm.setDustHi_Day(cursor.getFloat(cursor.getColumnIndex("DustHi_Day")));
		alarm.setDustHi_Night(cursor.getFloat(cursor.getColumnIndex("DustHi_Night")));
		alarm.setDustDelay(cursor.getInt(cursor.getColumnIndex("DustDelay")));
		
		alarm.setNoiseAlarmType(cursor.getString(cursor.getColumnIndex("NoiseAlarmType")));
		alarm.setNoiseHi_Day(cursor.getFloat(cursor.getColumnIndex("NoiseHi_Day")));
		alarm.setNoiseHi_Night(cursor.getFloat(cursor.getColumnIndex("NoiseHi_Night")));
		alarm.setNoiseDelay(cursor.getInt(cursor.getColumnIndex("NoiseDelay")));
		
		alarm.setDayTimeStart(cursor.getString(cursor.getColumnIndex("DayTimeStart")));
		alarm.setNightTimeStart(cursor.getString(cursor.getColumnIndex("NightTimeStart")));
		return alarm;
	}
}
